import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib

matplotlib.rc("font", family='Source Han Sans CN', weight="regular", size="10")

file_path = "csv/911.csv"
df = pd.read_csv(file_path)

title_series = df["title"].str.split(":")
title_list = [i[0] for i in title_series]

# 为原数据新增一列分类，再通过分组聚合统计数量
df["category"] = np.array(title_list).reshape((df.shape[0], 1))
title_count = df.groupby(by="category")["title"].count()

plt.xlabel("类型")
plt.ylabel("数量")
plt.title("911电话报警类型统计图", weight="bold", size="16")
plt.xticks(range(3), list(set(title_list)))

plt.bar(range(3), [title_count["EMS"], title_count["Fire"], title_count["Traffic"]])
plt.show()
